import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;

import junit.framework.TestCase;

public class AutoPlayerTest extends TestCase
{
	private BoardMaker myMaker;
	private LexiconInterface myLexicon;
	private AutoPlayerLexiconFirst myPlayer;
	private Scanner mySmallDictionary;

	/**
	 * Code that is run before each test method (automatically)
	 */
	public void setUp()
	{
		// Open the dictionary
		try
		{
			mySmallDictionary = new Scanner(new File("smalltestwords.txt"));
		}
		catch (FileNotFoundException e)
		{
			System.out.println(e);
			assertTrue(false);
		}

		// Set up the instance variables
		myLexicon = new LexiconArrayList();
		myLexicon.load(mySmallDictionary);
		myMaker = new BoardMaker();
		myPlayer = new AutoPlayerLexiconFirst(myLexicon);
	}

	/**
	 * Closes smalltestwords.txt after each test.
	 */
	public void tearDown()
	{
		mySmallDictionary.close();
	}

	/**
	 * This tests that we walk along the corners successfully. It is a very basic test- if you pass it, you should not assume that your code will work in all cases.
	 */
	public void testFindAllWords()
	{
		// This is how you can create a test board:
		String[] boardContents = { "qqqq", "qilt", "ated", "duck", };
		BoggleBoard board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 5);
		Iterator<String> i = myPlayer.iterator();
		assertEquals(i.next(), "a");
		assertEquals(i.next(), "dated");
		assertEquals(i.next(), "duck");
		assertEquals(i.next(), "quil");
		assertEquals(i.next(), "quilt");
		assertFalse(i.hasNext());

		myLexicon = new LexiconTrie();
		try
		{
			mySmallDictionary = new Scanner(new File("smalltestwords.txt"));
		} catch (FileNotFoundException e)
		{
			System.out.println(e);
			assertTrue(false);
		}
		myLexicon.load(mySmallDictionary);
		myPlayer.clear();
		myPlayer.findAllValidWords(board, myLexicon, 4);

		assertEquals(myPlayer.wordCount(), 5);
		i = myPlayer.iterator();
		assertEquals(i.next(), "a");
		assertEquals(i.next(), "dated");
		assertEquals(i.next(), "duck");
		assertEquals(i.next(), "quil");
		assertEquals(i.next(), "quilt");
		assertFalse(i.hasNext());
	}

	/**
	 * This tests if prefixes/words can be correctly found in the lexicon. This is a catch all test to make sure that the lexicon being used in this test suite is correctly implemented.
	 */
	public void testLexicon()
	{
		assertTrue(myLexicon.containsPrefix("dat"));
		assertTrue(myLexicon.containsPrefix("duc"));
		assertTrue(myLexicon.containsPrefix("gor"));
		assertTrue(myLexicon.containsPrefix("date"));
		assertTrue(myLexicon.containsPrefix("dated"));
		assertTrue(myLexicon.containsPrefix("quil"));
		assertTrue(myLexicon.containsPrefix("quilt"));
		assertFalse(myLexicon.contains("du"));
		assertTrue(myLexicon.contains("a"));
		assertTrue(myLexicon.contains("big"));
		assertTrue(myLexicon.contains("cat"));
		assertTrue(myLexicon.contains("dated"));
		assertTrue(myLexicon.contains("duck"));
		assertTrue(myLexicon.contains("every"));
		assertTrue(myLexicon.contains("fat"));
		assertTrue(myLexicon.contains("gore"));
		assertTrue(myLexicon.contains("gorilla"));
		assertTrue(myLexicon.contains("quilt"));
		assertTrue(myLexicon.contains("quil"));
	}
}